#!/bin/bash
#功能函数
funcs=`which functions.sh`
if [ "$funcs" != "" ] ;then
    . $funcs
else
    funcs=`dirname $0`/functions.sh
    echo "$PWD"
    echo "funcs=$funcs"
    if [ "$funcs" != "" ] ;then
	. $funcs
    else
	exit
    fi
fi

mtk_hdr_len=512
mtk_boot_tag=ROOTFS
mtk_recovery_tag=RECOVERY
MKBOOTFS=out/host/linux-x86/bin/mkbootfs
MINIGZIP=out/host/linux-x86/bin/minigzip 
MKBOOTIMG=out/host/linux-x86/bin/mkbootimg
MKIMAGE=mediatek/build/tools/mkimage
ROOT_OUT=out/target/product/$product_name/root
KERNEL=out/target/product/$product_name/kernel
RAMDISK=out/target/product/$product_name/ramdisk.img
BOOTIMG=out/target/product/$product_name/boot.img

function repack_boot()
{
  echoTextBlue "boot repacking ......"
}

function repack_ramdisk()
{
  echoTextBlue "ramdisk repacking ......"
}

function unpack_ramdisk()
{
  echoTextBlue "ramdisk unpacking ......"
  local input=$1
  local tmp_input=/tmp/input
  local output=$2
  local tag=
  echoTextGreen "input=$input output=$output"
  if [ -z "$input" ] || [ -z "$output" ]; then
    echoTextRed "指定要处理的文件和输出目录!!"
    usage
    return;
  fi
  if test ! -d $output; then
    mkdir -pv $output
  fi
  cp $input $tmp_input
  tag="`hexmodify -s 8 -n 8 -i $tmp_input`"
  tag="`sh_HexToCh "$tag"`"
  cd $output
  if [ "$tag" = "ROOTFS" ]; then
    dd if=$tmp_input  bs=512 skip=1 of=/tmp/ramdisk-ROOTFS.gz
    cp /tmp/ramdisk-ROOTFS.gz $tmp_input -rf
  fi 
  tag=1f8b08
  tag=`hexmodify -s 0 -n 4 -i $tmp_input | tr -d ' ' | grep $tag`
  if [ -z "$tag" ]; then
    echoTextRed "$input 格式有误，不具备 ramdisk 文件特性!!"
    return
  else
    gzip -dc $tmp_input | cpio -i
  fi
  cd -
  cp $tmp_input  $output/../
}

function unpack_boot()
{
  echoTextBlue "boot unpacking ......"
  local input=$1
  local output=$2
  local tag=
  echoTextGreen "input=$input output=$output"
  if [ -z "$input" ] || [ -z "$output" ]; then
    echoTextRed "指定要处理的文件和输出目录!!"
    usage
    return;
  fi
  if [ -n "$input" ] && test -f $input; then
    if test ! -d $output; then
      mkdir $output -pv
    else
    result=`question "目录 $output 已经存在,是否覆盖？" 1 yes` 
      if [  "$result" = "no" ] ; then
        return
      fi
    fi
    unpackbootimg -i $input -o $output/
    unpack_ramdisk $output/boot.img-ramdisk.gz  $output/ramdisk 
  else
     echo "$img not exist!!"
     return
  fi
}
function usage()
{
  local cmd=`basename $0`
echoTextGreen "使用说明:"
echoTextRed "$cmd --unpack-ramdisk -i ramdisk.gz/ramdisk.img  -o output 
$cmd --unpack-boot    -i boot.img                -o output 
$cmd --repack-ramdisk -i root_dir                -o output 
$cmd --repack-boot  --kernel kernel  --ramdisk ramdisk.img  -o output 
"
}
function check_env()
{
MKBOOTFS=out/host/linux-x86/bin/mkbootfs
MINIGZIP=out/host/linux-x86/bin/minigzip 
MKBOOTIMG=out/host/linux-x86/bin/mkbootimg
MKIMAGE=mediatek/build/tools/mkimage
ROOT_OUT=out/target/product/$product_name/root
KERNEL=out/target/product/$product_name/kernel
RAMDISK=out/target/product/$product_name/ramdisk.img
BOOTIMG=out/target/product/$product_name/boot.img
}

input=
output=
#脚本入口
while test $# != 0
do
    case $1 in
 	-m)
	    shift
	    mode=$1
	    ;; 
	-i)
	    shift
	    input=$1
	    ;; 
	-o)
	    shift
	    output=$1
	    ;; 
	--unpack-ramdisk)
	    func=unpack_ramdisk
	    ;;
	--unpack-boot)
	    func=unpack_boot
	    ;;
	--repack-ramdisk)
	    func=repack_ramdisk
	    ;;
	--repack-boot)
	    func=repack_boot
	    ;;
	--help|-help)
	    usage
	    exit
	    ;;
        *)
	   work_copy=$1
	   ;;
       esac
       shift
done

echo "func=$func"
if [ -z "$func" ]; then
  usage
  exit
fi
echo "$func $input $output"
$func $input $output



